var petname = $.cookie('petname')

if (petname) {
    $("#user").find('span').last().text(petname)
}

$('#ask').click(function(){
    if (petname) {
        location.href = 'ask.html'
    } else location.href = 'signin.html'
})

$.get('/index',null,function(res){
    console.log(res)

    var html = template('question-template',{
        q:res.data
    })
    $('.questions').html(html)
})

$('.questions').on('click','.media',function(){
    if (petname) {
        // 先把问题的id写入cookie
        // console.log($(this).attr('question'))
        $.cookie('file', $(this).attr('question'))

        // 再跳转到回答页面
        location.href = 'answer.html'
    } else location.href = 'signin.html'
})

// 模板的过滤器，作用：把时间转为毫秒数
template.defaults.imports.ms = function(t){
    t = new Date(t)
    return t.getTime()
}